﻿using System;
using System.Collections.Generic;
using AMM.Business.Finance;
using AMM.Model;
using AMM.Model.Finance;

namespace AMM.UserControls {
    public partial class CostSearchControl : BaseSearchControl {

        public override BaseSearchModel GetSearchModel() {
            int costType = this.cmbCostType.SelectedIndex;
            int statementId = Convert.ToInt32(this.ddlStatement.SelectedValueEx);
            string accountNumber = Convert.ToString(this.ddlBankAccount.SelectedValueEx);

            CostSearchModel searchResult = new CostSearchModel();
            searchResult.CostType = costType;
            searchResult.StatementId = statementId;
            searchResult.AccountNumber = accountNumber;
            searchResult.Remarks = this.tbRemarks.Text;
            searchResult.CreateDateStart = this.GetDateTimePickerValue(this.dtpCreateStart);
            searchResult.CreateDateEnd = this.GetDateTimePickerValue(this.dtpCreateEnd);

            if (costType == -1 || costType == 0) { searchResult.CostType = null; }
            if (statementId == 0) { searchResult.StatementId = null; }

            return searchResult;
        }

        public override void ResetSearchModel() {
            this.tbRemarks.Clear();
            this.cmbCostType.SelectedIndex = 0;
            this.ddlStatement.SelectedValueEx = null;
            this.ddlBankAccount.SelectedValueEx = null;
        }

        public CostSearchControl() {
            this.InitializeComponent();
        }

        private void BindStatement() {
            StatementSearchModel searchModel = new StatementSearchModel() { Status = 2 };
            StatementProcessor statementProcessor = new StatementProcessor(int.MaxValue);
            List<StatementViewModel> statementList = statementProcessor.FetchStatementViewModelList(searchModel);

            this.ddlStatement.DisplayMemberEx = "StatementCode";
            this.ddlStatement.ValueMemberEx = "Key";
            this.ddlStatement.HeightEx = 300;
            this.ddlStatement.WidthEx = 400;
            this.ddlStatement.PageSizeEx = 20;
            this.ddlStatement.DataBind(new StatementViewModel(), x => new { x.StatementCode, x.VendorCode, x.VendorName }, statementList);
        }

        private void BindBankAccount() {
            BankAccountSearchModel searchModel = new BankAccountSearchModel();
            BankAccountProcessor bankAccountProcessor = new BankAccountProcessor(int.MaxValue);
            List<BankAccountViewModel> bankAccountList = bankAccountProcessor.FetchBankAccountViewModelList(searchModel);

            this.ddlBankAccount.DisplayMemberEx = "BankAccountNumber";
            this.ddlBankAccount.ValueMemberEx = "BankAccountNumber";
            this.ddlBankAccount.HeightEx = 300;
            this.ddlBankAccount.WidthEx = 400;
            this.ddlBankAccount.PageSizeEx = 20;
            this.ddlBankAccount.DataBind(new BankAccountViewModel(), x => new { x.Code, x.BankAccountName, x.BankAccountNumber }, bankAccountList);
        }

        private void CostSearchControl_Load(object sender, EventArgs e) {
            this.BindStatement();
            this.BindBankAccount();
        }
    }
}
